1 package edu.jiangxin.apktoolbox.swing.extend.listener; 2 3 import edu.jiangxin.apktoolbox.utils.FileUtils; 4 5 import javax.swing.*; 6 import javax.swing.filechooser.FileFilter; 7 import java.awt.event.ActionEvent; 8 import java.awt.event.ActionListener; 9 import java.io.File; 10 11 public class SelectFileListener implements ActionListener { 12 13 private final String dialogTitle; 14 private final JTextField pathTextField; 15 16 private FileFilter fileFilter; 17 18 public SelectFileListener(String dialogTitle, JTextField pathTextField) { 19 super(); 20 this.dialogTitle = dialogTitle; 21 this.pathTextField = pathTextField; 22 } 23 24 public SelectFileListener(String dialogTitle, JTextField pathTextField, FileFilter fileFilter) { 25 super(); 26 this.dialogTitle = dialogTitle; 27 this.pathTextField = pathTextField; 28 this.fileFilter = fileFilter; 29 } 30 31 @Override 32 public void actionPerformed(ActionEvent e) { 33 JFileChooser jfc = new JFileChooser(); 34 jfc.setFileSelectionMode(JFileChooser.FILES_ONLY); 35 jfc.setDialogTitle(dialogTitle); 36 if (fileFilter != null) { 37 jfc.setFileFilter(fileFilter); 38 } 39 int ret = jfc.showDialog(new JLabel(), null); 40 if (ret == JFileChooser.APPROVE_OPTION) { 41 File file = jfc.getSelectedFile(); 42 String path = FileUtils.getCanonicalPathQuiet(file); 43 pathTextField.setText(path); 44 } 45 } 46 }